﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Telerik.Web.UI;
using System.Threading;
using BSCMFF.Core.BusinessLayer;
using BSCMFF.Core.DataAccessLayer.DataObjects;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web
{
    /// <summary>
    /// Master page. 
    /// </summary>
    /// <remarks></remarks>
    public partial class MainSite : System.Web.UI.MasterPage
    {
        /// <summary>
        /// 
        /// </summary>
        public CompanyInfo ci;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["loggedUserID"] == null)
                {
                    string login = HttpContext.Current.User.Identity.Name;

                    BSCEntities context = new BSCEntities();

                    User user = (from u in context.Users
                                 where u.Login == login
                                 select u).SingleOrDefault();

                    if (user != null)
                    {
                        Session.Add("loggedUserID", user.ID);
                    }
                }
                
                string role = ShowRoles();

                AdjustMenuVisibilityAccordintToRole(role);

                KeepPanelItemOpen();
                
                ShowLogo();

                HttpCookie cookie = Request.Cookies["Theme"];
                if (cookie != null)
                {
                    RadSkinManager1.Skin = cookie.Value;
                }
                
            }
        }

        /// <summary>
        /// Adjusts the menu visibility accordint to role.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <remarks></remarks>
        private void AdjustMenuVisibilityAccordintToRole(string role)
        {
            switch (role)
            {
                case "Administrator": AdjustAdmin(); break;
                case "Manager": AdjustManager(); break;
                case "Personalist": AdjustPersonalist(); break;
                case "Employee": AdjustEmployee(); break;
                default: break;
            }
        }

        /// <summary>
        /// Adjusts the employee.
        /// </summary>
        /// <remarks></remarks>
        private void AdjustEmployee()
        {
            RadPanelBar1.FindItemByValue("Admin").Visible = false;

            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BusinessGoalWizard").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("TargetsManagement").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("StrategyMap").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BSCProgress").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BulkCascade").Visible = false;

            RadPanelBar1.FindItemByValue("Survey").Items.FindItemByValue("ManageSurveys").Visible = false;

            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("ManageEvaluations").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("EvaluationNotifications").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("AssignEmployeesforEvaluation").Visible = false;
        }

        /// <summary>
        /// Adjusts the personalist.
        /// </summary>
        /// <remarks></remarks>
        private void AdjustPersonalist()
        {
            RadPanelBar1.FindItemByValue("Admin").Visible = false;

            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BusinessGoalWizard").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("TargetsManagement").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("StrategyMap").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BSCProgress").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BulkCascade").Visible = false;
        }

        /// <summary>
        /// Adjusts the manager.
        /// </summary>
        /// <remarks></remarks>
        private void AdjustManager()
        {
            RadPanelBar1.FindItemByValue("Admin").Visible = false;

            RadPanelBar1.FindItemByValue("Survey").Items.FindItemByValue("ManageSurveys").Visible = false;

            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("ManageEvaluations").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("EvaluationNotifications").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("AssignEmployeesforEvaluation").Visible = false;
        }

        /// <summary>
        /// Adjusts the admin.
        /// </summary>
        /// <remarks></remarks>
        private void AdjustAdmin()
        {
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BusinessGoalWizard").Visible=false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("TargetsManagement").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("StrategyMap").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BSCProgress").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BulkCascade").Visible = false;

            RadPanelBar1.FindItemByValue("Survey").Items.FindItemByValue("ManageSurveys").Visible = false;

            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("ManageEvaluations").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("EvaluationNotifications").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("AssignEmployeesforEvaluation").Visible = false;
        }

        /// <summary>
        /// Shows the logo.
        /// </summary>
        /// <remarks></remarks>
        private void ShowLogo()
        {
            if (!IsPostBack)
            {
                CompanyInfoManager cim = new CompanyInfoManager();

                ci = cim.GetCompanyInfo();
                RadBinaryImageMasterLogo.DataValue = ci.CompanyLogo;
                RadBinaryImageMasterLogo.DataBind();
            }
        }

        /// <summary>
        /// Shows the roles.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        private string ShowRoles()
        {
            string[] roles = Roles.GetRolesForUser();

            LabelUserRoles.Text = "";

            foreach (string r in roles)
            {
                LabelUserRoles.Text += r + " ";

                HideMenuItemsForRole(r);
            }
            return roles[0];
        }

        /// <summary>
        /// Hides the menu items for role.
        /// </summary>
        /// <param name="r">The r.</param>
        /// <remarks></remarks>
        private void HideMenuItemsForRole(string r)
        {
            switch (r)
            {
                case "Administrator": ShowAdmin(); break;
                case "Manager": ShowManager(); break;
                case "Employee": ShowEmployee(); break;
                default: break;
            }
        }

        /// <summary>
        /// Shows the employee.
        /// </summary>
        /// <remarks></remarks>
        private void ShowEmployee()
        {
            RadPanelItem rpi = RadPanelBar1.Items.FindItemByValue("Admin");
            rpi.Visible = false;
        }

        /// <summary>
        /// Shows the manager.
        /// </summary>
        /// <remarks></remarks>
        private void ShowManager()
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// Shows the admin.
        /// </summary>
        /// <remarks></remarks>
        private void ShowAdmin()
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// Keeps the panel item open.
        /// </summary>
        /// <remarks></remarks>
        private void KeepPanelItemOpen()
        {
            // ApplyAppPathModifier will add the session ID if we're using Cookieless session.
            string urlWithSessionID = Response.ApplyAppPathModifier(Request.Url.PathAndQuery);
            RadPanelItem clickedItem = RadPanelBar1.FindItemByUrl(urlWithSessionID);

            // Expand the parent of the clicked item
            if (clickedItem != null)
            {
                clickedItem.ExpandParentItems();
                ShowPath(clickedItem);
            }
        }

        /// <summary>
        /// Shows the path.
        /// </summary>
        /// <param name="clickedItem">The clicked item.</param>
        /// <remarks></remarks>
        private void ShowPath(RadPanelItem clickedItem)
        {
            foreach (RadPanelItem childItem in clickedItem.PanelBar.GetAllItems())
            {
                childItem.CssClass = "";
            }

            clickedItem.CssClass = "rpSelected";
        }

        /// <summary>
        /// Handles the Click event of the ImageButtonEnglish control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void ImageButtonEnglish_Click(object sender, ImageClickEventArgs e)
        {
            HttpCookie cookie = new HttpCookie("Culture");
            cookie.Value = "en-US";
            Response.Cookies.Add(cookie);

            Response.Redirect(Request.Url.AbsolutePath);
        }

        /// <summary>
        /// Handles the Click event of the ImageButtonSlovak control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void ImageButtonSlovak_Click(object sender, ImageClickEventArgs e)
        {
            HttpCookie cookie = new HttpCookie("Culture");
            cookie.Value = "sk-SK";
            Response.Cookies.Add(cookie);

            Response.Redirect(Request.Url.AbsolutePath);
        }

        /// <summary>
        /// Handles the LoggingOut event of the LoginStatus1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.LoginCancelEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            Session.Abandon();
        }
    }
}